package com.orders.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.orders.pojo.Orders;

public class OrdersOperation implements Operation {
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	private Orders orders;
	@Override
	public boolean insert(Object obj) {
		try {
			if(obj instanceof Orders){
				orders = (Orders)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("insert into orders values (?,?,?,?,?,?)");
				ps.setInt(1, orders.getId());
				ps.setString(2, orders.getOrder_number());
				ps.setDate(3, orders.getOrder_time());
				ps.setString(4, orders.getStatus());
				ps.setInt(5, orders.getCustomer().getId());
				ps.setBoolean(6, orders.isVisible());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return false;
	}

	@Override
	public boolean update(Object obj) {
		try {
			if(obj instanceof Orders){
				orders = (Orders)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update orders set order_number = ?,order_time = ?,status = ?,customer_id = ?,visible = ? where id = ?");
				ps.setString(1, orders.getOrder_number());
				ps.setDate(2, orders.getOrder_time());
				ps.setString(3, orders.getStatus());
				ps.setInt(4, orders.getCustomer().getId());
				ps.setBoolean(5, orders.isVisible());
				ps.setInt(6, orders.getId());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return false;
	}

	@Override
	public List select(Object obj) {
		List list = new ArrayList<>();
		CustomerOperation co = new CustomerOperation();
		try {
			if(obj instanceof Orders){
				orders = (Orders)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from orders where id = ? and visible = 1");
				ps.setInt(1, orders.getId());
				rs = ps.executeQuery();
				while(rs.next()){
					orders.setId(rs.getInt("id"));
					orders.setOrder_number(rs.getString("order_number"));
					orders.setOrder_time(rs.getDate("order_time"));
					orders.setCustomer(co.selectById(rs.getInt("customer_id")));
					orders.setStatus(rs.getString("status"));
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return list;
	}

	public Orders selectById(int id) {
		CustomerOperation co = new CustomerOperation();
		orders = new Orders();
		try {
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from orders where id = ? and visible = 1");
				ps.setInt(1, id);
				rs = ps.executeQuery();
				rs.next();
					orders.setId(rs.getInt("id"));
					orders.setOrder_number(rs.getString("order_number"));
					orders.setOrder_time(rs.getDate("order_time"));
					orders.setCustomer(co.selectById(rs.getInt("customer_id")));
					orders.setStatus(rs.getString("status"));
				
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return orders;
	}
	
	@Override
	public boolean delete(Object obj) {
		try {
			if(obj instanceof Orders){
				orders = (Orders)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update orders set visible = ? where id = ?");
				ps.setInt(2, orders.getId());
				ps.setBoolean(1, false);
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return false;
	}

}
